<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="过程质量监控分类">
          <el-select
            v-model="searchParam.abilityType"
            placeholder="请选择过程监控分类"
            clearable
          >
            <el-option
              v-for="item in useDictionary('AbilityTypeMin')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="reset">
            <i-ep-refresh />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()">
          <i-ep-plus />
          新增
        </el-button>
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        :maxHeight="tableHeight"
      >
        <el-table-column
          label="过程质量监控分类"
          prop="abilityType"
          align="center"
          min-width="120"
        >
          <template #default="scope">
            {{ useDictionary("AbilityTypeMin", scope.row.abilityType) }}
          </template>
        </el-table-column>

        <el-table-column
          label="检具用途"
          prop="toolUseType"
          min-width="140"
          show-overflow-tooltip
          header-align="center"
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("samplingStatus", scope.row.toolUseType ?? "") }}
          </template>
        </el-table-column>
        <el-table-column
          label="监控描述"
          prop="memo"
          min-width="170"
          show-overflow-tooltip
          header-align="center"
          align="right"
        >
          <template #default="scope">
            {{ scope.row.memo }}
          </template>
        </el-table-column>

        <!-- <el-table-column
          label="启用状态"
          prop="enabled"
          min-width="100"
          align="center"
        >
          <template #default="scope">
            <el-switch
              v-model="scope.row.enabled"
              inline-prompt
              active-text="启用"
              inactive-text="禁用"
              :active-value="1"
              :inactive-value="0"
              :loading="switchLoading"
              @click="changeEnabled(scope.row)"
            />
          </template>
        </el-table-column> -->
        <el-table-column label="是否启用" align="center" min-width="100">
          <template #default="scope">
            <el-tag v-if="scope.row.enabled === 1" type="success">启用</el-tag>
            <el-tag v-else type="info">停用</el-tag>
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="110" align="center">
          <template #default="scope">
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="查看"
              placement="top"
              class="mmm1"
              popper-class="mmm2"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openDialogdetail(scope.row)"
              >
                <i-ep-search />
              </el-button>
            </el-tooltip>

            <el-tooltip
              :hide-after="0"
              effect="light"
              content="编辑"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openDialog(scope.row)"
              >
                <i-ep-edit />
              </el-button>
            </el-tooltip>
            <el-tooltip
              :hide-after="0"
              effect="light"
              :content="scope.row.enabled == '1' ? '使用中' : '删除'"
              placement="top"
            >
              <el-button
                :disabled="scope.row.enabled == '1'"
                type="primary"
                size="small"
                link
                @click="deleteItem(scope.row.id)"
              >
                <i-ep-delete />
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <pcmconfigmeasurements-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :abilityType="searchParam.abilityType"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : getTableList())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import {
  useTable,
  useDownload,
  useDictionary,
  useComputeTableHeight,
} from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "PCMPCMCONFIGMEASUREMENTS",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/dmpDosPcm/pcmconfigmeasurements/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  countRuleCode: "",
  abilityType: "0",
  maxUnit: "",
  enabled: "",
  deleted: "",
  createTimeSearch: [],
  createTimeStart: "",
  createTimeEnd: "",
  lastUpdateTimeSearch: [],
  lastUpdateTimeStart: "",
  lastUpdateTimeEnd: "",
};

const deleteParam = {
  api: "/dmpDosPcm/pcmconfigmeasurements/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const tableHeight = ref();

function dataCallBack(data: any) {
  setTimeout(() => {
    tableHeight.value = useComputeTableHeight();
  }, 100);
  return data;
}

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/dmpDosPcm/pcmconfigmeasurements/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开测量数据取样配置表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "产品特性配置规则-编辑";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "产品特性配置规则-新增";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开测量数据取样配置表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "产品特性配置规则-详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function getcreateTime(data: any) {
  if (data) {
    searchParam.value.createTimeStart = data[0];
    searchParam.value.createTimeEnd = data[1];
  } else {
    searchParam.value.createTimeStart = "";
    searchParam.value.createTimeEnd = "";
  }
}

function getlastUpdateTime(data: any) {
  if (data) {
    searchParam.value.lastUpdateTimeStart = data[0];
    searchParam.value.lastUpdateTimeEnd = data[1];
  } else {
    searchParam.value.lastUpdateTimeStart = "";
    searchParam.value.lastUpdateTimeEnd = "";
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await request({
      url: `/dmpDosPcm/pcmconfigmeasurements/deleteItem/${id}`,
      method: "post",
    }).then(() => {
      ElMessage.success("删除成功！");
      search();
    });
  });
}

// const switchLoading = ref(false);

// async function changeEnabled(row: any) {
//   switchLoading.value = true;
//   const { data } = await request({
//     url: "/dmpDosPcm/pcmconfigmeasurements/changeEnabled",
//     method: "post",
//     data: {
//       id: row.id,
//       enabled: row.enabled,
//     },
//   }).finally(() => {
//     switchLoading.value = false;
//   });
//   search();
// }
</script>
